# 类对象(浅-深)拷贝
'''
浅拷贝：
    定义p1:{'name': '张三', 'age': 23}
    将p1对象赋值p2, 这时修改p2.name = '丽丽',
    查看p2.name已经是丽丽了，但p1.name也变成了丽丽；(这就是浅拷贝！！)'''

class Cpu:
    def __init__(self, name):
        self.name = name
class Disk:
    def __init__(self, name):
        self.name = name
class Computer:
    def __init__(self, cpu, disk):
        self.cpu = cpu
        self.disk = disk

c = Cpu('InterII')
d = Disk('金士顿')
computer1 = Computer(c, d)


# 使用浅拷贝
import copy
print('------------------------浅拷贝------------------------------')
print('computer1: ', computer1, computer1.cpu, computer1.disk)
computer2 = copy.copy(computer1)    #将 computer1 拷贝一份给 computer2
print('computer2: ', computer2, computer2.cpu, computer2.disk)

print(computer1.cpu.name)
print(computer2.cpu.name)
# # 修改computer2.name 发现 computer1.name也变了；
# computer2.cpu.name = 'AMD7'
# print('computer1.cpu.name ', computer1.cpu.name)
# print('computer2.cpu.name ', computer2.cpu.name)

#执行结果
'''
computer1:  <__main__.Computer object at 0x0000023200A5BDF0> <__main__.Cpu object at 0x0000023200A5BFD0> <__main__.Disk object at 0x0000023200A5BF70>
computer2:  <__main__.Computer object at 0x0000023200A5BC70> <__main__.Cpu object at 0x0000023200A5BFD0> <__main__.Disk object at 0x0000023200A5BF70>
InterII
InterII
computer1.cpu.name  AMD7
computer2.cpu.name  AMD7
'''

print('------------------------深拷贝------------------------------')
computer3 = copy.deepcopy(computer1)
print('computer1: ', computer1, computer1.cpu, computer1.disk)
print('computer3: ', computer3, computer3.cpu, computer3.disk)
# 修改computer2.name 发现 computer1.name也变了；
computer3.cpu.name = 'AMD7'
print('computer1.cpu.name ', computer1.cpu.name)
print('computer3.cpu.name ', computer3.cpu.name)
'''
computer1:  <__main__.Computer object at 0x0000029B43D0C670> <__main__.Cpu object at 0x0000029B43D0C850> <__main__.Disk object at 0x0000029B43D0C7F0>
computer3:  <__main__.Computer object at 0x0000029B43D0C430> <__main__.Cpu object at 0x0000029B43D0C040> <__main__.Disk object at 0x0000029B43D2E730>
computer1.cpu.name  InterII
computer3.cpu.name  AMD7
'''
